var formArea = "formulario";
var formTable = "listado";
var dialogArea = "divdialog";
var tableContainer = "tabla";
var maintenanceCfg = null;
var runningTable = null;
var globalCallBack = null;
var paramStat = "";
var rsv = null;
var TabbedPanels;
var listF = new Array();
var modal = null;

addOnLoad = function(o) {
    listF.push(o);
};
waitMsg = function(msg) {
    modal = new Dialog({
        title: 'SoftCollege',
        content: msg,
        close: {
            link: false,
            overlay: false
        }
    });
    modal.open();
};

closeWait = function() {
    Dialogs.close();
};

loader = function() {
    listF.each(function(f) {
        f();
    });
};

window.onload = loader;

/**
 * 
 * @param {type} e element observer
 * @param {type} cfg {'table',[{'t1':'f1'},{'t1':'f1'}]}
 * @returns {undefined}
 */
function setList(e, cfg) {
    var d = new Element('div');
    d.hide();
    d.id = dialogArea;
    d.update('Cargando...');
    $(document.body).appendChild(d);

    bootbox.dialog({
        message: "Cargando",
        title: "Custom title",
        buttons: {
            success: {
                label: "Success!",
                className: "btn-success",
                callback: function() {
                    Example.show("great success");
                }
            },
            danger: {
                label: "Danger!",
                className: "btn-danger",
                callback: function() {
                    Example.show("uh oh, look out!");
                }
            },
            main: {
                label: "Click ME!",
                className: "btn-primary",
                callback: function() {
                    Example.show("Primary button");
                }
            }
        }
    });

    modal = new Dialog({
        title: cfg.title,
        content: function() {
            return $(dialogArea);
        },
        close: {
            link: true,
            overlay: false,
            esc: true
        },
        width: cfg.width,
        height: cfg.height,
        afterOpen: function() {
            fillDialog(cfg);
            $(dialogArea).show();
        },
        afterClose: function() {
            $(dialogArea).hide();
        }
    });
    if (e !== null) {
        e.observe('click', function() {
            modal.open();
        });
    } else {
        modal.open();
    }
}
;

function fillDialog(c) {
    if (c.ajax) {
        new Ajax.Updater(dialogArea, c.url);
    } else {
        new TableOrderer(dialogArea, {
            url: c.url,
            filter: 'top',
            callBack: function(s) {
                c.callback(s);
                Dialogs.close();
            }
        });
    }
}

getFrecuencia = function(tipo) {
    switch (parseInt(tipo)) {
        case 1:
            return "Mensual";
        case 2:
            return "Anual";
    }
};

function loadPage(url, params) {
    waitMsg("Cargando");
    new Ajax.Updater(formArea, url, {
        parameters: params,
        method: 'GET',
        onComplete: function(r) {
            $(tableContainer).style.display = "none";
            $(formArea).style.display = "block";
            globalCallBack();
            closeWait();
            $$("input").invoke("stopObserving", "mouseout");
            $$("input").invoke("stopObserving", "mouseover");
            $$("input").invoke("observe", "mouseout", classChange);
            $$("input").invoke("observe", "mouseover", classChange);

        }
    });

}


function loadLocal(url, params) {
    waitMsg("Guardando...");
    window.location = url + '?' + params;
    closeWait();
}

function formOkBt(form, handler) {
    if (rsv !== null) {
        rsv.options.onCompleteHandler = handler;
        rsv.validate();
    }
}

function newAction(form) {
    waitMsg("Guardando...");
    $(form).request({
        onSuccess: function(r) {
            loadTable();
            if (globalCallBack)
                globalCallBack();
            closeWait();
        } //function
    }//end params
    );
    return false;
}

function updateAction(form) {
    if (confirm("Desea guardar los cambios?")) {
        waitMsg("Actualizando...");
        $(form).request({
            onSuccess: function(r) {
                $('error').update(r.responseText);
                loadTable();
                closeWait();
            }
        });
    }
}


function deleteAction(url, param) {
    if (confirm("Esta seguro de realizar esta operacion?")) {
        waitMsg("Eliminando");
        $('error').update(param);
        new Ajax.Request(url, {
            parameters: param,
            method: 'GET',
            onSuccess: function(r) {
                loadTable();
                closeWait();
            }
        });
    }
}

function loadTable() {
    runningTable = new TableOrderer('tabla', {
        url: maintenanceCfg.jsonUrl,
        callBack: maintenanceCfg.callBack,
        filter: 'top'
    });
    $(tableContainer).style.display = "block";
    $(formArea).style.display = "none";
}

function getContent(idElement, url, params) {
    new Ajax.Updater(idElement, url, {
        method: 'GET',
        parameters: params
    });
}

function parseDate(stringdate) {
    var sep = '-';
    var Ano = new String(stringdate.substring(0, stringdate.indexOf(sep)));
    var Mes = new String(stringdate.substring(stringdate.indexOf(sep) + 1, stringdate.lastIndexOf(sep)));
    var Dia = new String(stringdate.substring(stringdate.lastIndexOf(sep) + 1, stringdate.length));
    var date = new Date();
    date.setFullYear(Ano, Mes, Dia);
    return date;
}

setObservers = function() {
    /* $$("input").invoke("stopObserving", "mouseout");
     $$("input").invoke("stopObserving", "mouseover");
     $$("input").invoke("observe", "mouseout", classChange);
     $$("input").invoke("observe", "mouseover", classChange);*/
};

addOnLoad(setObservers);

classChange = function(e) {
    var o = Event.element(e);

    if (o.type === "button" || o.type === "submit") {
        o.className = (o.className.indexOf("Over") > 0) ? o.className.substr(0, o.className.indexOf("Over")) : (o.className + "Over");
    }

};
var loading = new Dialog({
    content: '<img width="396" height="316" src="img/loading.gif"/>',
    close: {
        link: false,
        overlay: false,
        esc: false
    }
});
